package com.qiku.controller;

import com.qiku.annotation.SystemLog;
import com.qiku.entity.LoginUser;
import com.qiku.entity.Menu;
import com.qiku.entity.ResponseResult;
import com.qiku.entity.User;
import com.qiku.service.BlogLoginService;
import com.qiku.service.LoginService;
import com.qiku.service.MenuService;
import com.qiku.service.RoleService;
import com.qiku.utils.BeanCopyUtils;
import com.qiku.utils.SecurityUtils;
import com.qiku.vo.AdminUserInfoVo;
import com.qiku.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class BlogLoginController {

    @Autowired
    private BlogLoginService blogLoginService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        ResponseResult result = blogLoginService.login(user);
        return result;
    }

    @GetMapping("/getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo(@RequestBody User user){
        //获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据用户id查询权限信息
        List<String> permsList = menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户id查询角色信息
        List<String> roleKeyList =roleService.selectRoleKeyByUserId(loginUser.getUser().getId());
        //获取用户信息
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(loginUser.getUser(), UserInfoVo.class);
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(permsList,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }
}
